<?php
$conn = @mysql_connect("localhost","root","root") or die('db');//数据库
@mysql_select_db("lions") or die('dbname');
mysql_set_charset("utf8", $conn);
$urlmodel=1;
$baseurl="http://localhost/";
$sitename="QQ空间那些事儿";
$sitenameadd="最惊爆的QQ日志";
$sitedescription="";   //站点描述
$updatecaiji=0;        //
$listupdatetime=0;   //
$keynumlimit=1;            //关键词限制
$keynummost=10;
$keynumrate=5/100;
$contentnumlimit=100;      //内容限制
$contentnummost=2000;
$matcharray=array(",",".");
$replacearray=array("dh<!--ssssdfssfss-->","<!--ssssdfssfss-->jh");
$lang['alltopic']="All Topic";//所有主题
$lang['time']="Time";//时间
$lang['description']="Description";//核心提示
$lang['thisarticle']="Title";//本文
$lang['author']="Author";//作者
$lang['source']="Source";//来源
$lang['nextarticle']="One";//下一篇
$lang['prearticle']="Two";//上一篇
$lang['copyright']="Copyright";//版权所有
$lang['latesarticle']="Latest Article";//最新文章
$lang['readmore']="View more";//阅读全文
function caiji($url,$pattern,$utf8=0)  //设置成0表示需要utf8  否则表示需要gbk
{
	$regs=array();
	$c=@file_get_contents($url);
	$c=$c&&$utf8?iconv("utf-8","gbk",$c):$c;
	if($c&&preg_match_all("/{$pattern}/",$c,$regs))
	{
		unset($c);
		array_shift($regs);
		return $regs;
	}
	else return false;
}
function fetweb_google($word,$wid,$url,$pattern,$utf8)
{
	global $keynumlimit,$keynummost,$keynumrate,$contentnummost,$contentnumlimit;
	$array=caiji($url,$pattern,$utf8);
	//echo $url;print_r($array);
	if($array&&is_array($array))
	{
		$n=count($array[0]);$k=0;
		for ($i=0;$i<$n;$i++)
		{
			/*
			 $titlewordnum=substr_count($array[0][$i],$word);//内容关键词个数
			 if($titlewordnum!=1) continue;//如果标题中没有关键词不要
			 $wordnum=substr_count($array[2][$i],$word);//内容关键词个数
			 if($wordnum<$keynumlimit||$wordnum>$keynummost) continue;//如果内容不匹配关键词的情况
			 $contentnum=strlen($array[2][$i]);//内容长度
			 if($contentnum>$contentnummost||$contentnum<$contentnumlimit) continue;//如果内容过长或过少
			 if(($wordnum/$contentnum)>$keynumrate) continue;//如果关键词密度过大
			 */
			$array[0][$i]=strip_tags(html_entity_decode($array[0][$i]));
			//if(!preg_match("/[\w\n]+/",$array[0][$i])) continue;//如果标题含有非英文
			//if(!preg_match("/[\w\n]+/",$array[3][$i])) continue;//如果正文含有非英文

			//$array[0][$i]=str_replace(array("!",":","|","),array(" "),$array[0][$i]);标题
			$array[0][$i]=str_replace(array("\"","...","."),array("-",""),$array[0][$i]);
			$array[3][$i]=strip_tags(html_entity_decode($array[3][$i]));
			$pinyin=str_replace(array(" ","...",".",":","\""),"-",substr($array[0][$i],0,20));

			mysql_query("INSERT INTO `article` ( `aid` , `wid` , `title` , `author` , `description`,`src`,`pubtime`,`pinyin` ) VALUES (NULL , '{$wid}', '{$array[0][$i]}', '{$array[3][$i]}', '','{$array[1][$i]}',now(),'{$pinyin}');");
			$id=mysql_insert_id();
			unset($array[0][$i],$array[3][$i],$pinyin);
			if($id)
			{	$k++;
			$array[2][$i]=html_entity_decode($array[2][$i]);
			$description=substr(strip_tags($array[2][$i]),0,200);
			mysql_query("UPDATE `article` SET `description` = '{$description}' WHERE `aid` ={$id} LIMIT 1 ;");
			unset($description);
			$array[2][$i]=preg_replace(array("/<p.*?>/i"),array("<p>　　"),strip_tags($array[2][$i],"<p><br>"));
			mysql_query("INSERT INTO `content` ( `aid` , `content` ) VALUES ('{$id}', '{$array[2][$i]}');");

			unset($array[2][$i]);
			}
		}
		mysql_query("UPDATE `word` SET `updatetime` = NOW( ) WHERE `wid` ={$wid} LIMIT 1 ;");

	}
}
function fetweb_yodao($word,$wid,$url,$pattern,$utf8,$seachsrc="yodao")
{
	global $keynumlimit,$keynummost,$keynumrate,$contentnummost,$contentnumlimit;
	$array=caiji($url,$pattern,$utf8);
	//echo $url;print_r($array);
	if($array&&is_array($array))
	{

		$n=count($array[0]);$k=0;
		for ($i=0;$i<$n;$i++)
		{
			if(preg_match("/.*\s-\s[A-Z]/",$array[0][$i])) continue;//去除作弊标题
			$titlewordnum=substr_count($array[0][$i],$word);//内容关键词个数
			if($titlewordnum!=1) continue;//如果标题中没有关键词不要
			$wordnum=substr_count($array[2][$i],$word);//内容关键词个数
			if($wordnum<$keynumlimit||$wordnum>$keynummost) continue;//如果内容不匹配关键词的情况
			$contentnum=strlen($array[2][$i]);//内容长度
			if($contentnum>$contentnummost||$contentnum<$contentnumlimit) continue;//如果内容过长或过少
			if(($wordnum/$contentnum)>$keynumrate) continue;//如果关键词密度过大
			$array[0][$i]=strip_tags(html_entity_decode($array[0][$i]));
			$array[0][$i]=str_replace(array(" ","-",":","|"),array(""),$array[0][$i]);
			$array[3][$i]=strip_tags(html_entity_decode($array[3][$i]));
			$pinyin=zh2pinyin(cnsubstr($array[0][$i],20));
			mysql_query("INSERT INTO `article` ( `aid` , `wid` , `title` , `author` , `description`,`src`,`pubtime`,`pinyin` ) VALUES (NULL , '{$wid}', '{$array[0][$i]}', '{$array[3][$i]}', '','{$array[1][$i]}',now(),'{$pinyin}');");
			$id=mysql_insert_id();
				
			unset($array[0][$i],$array[3][$i],$pinyin);
			if($id)
			{	$k++;
			$array[2][$i]=html_entity_decode($array[2][$i]);
			$description=cnsubstr(strip_tags($array[2][$i]),200);
			mysql_query("UPDATE `article` SET `description` = '{$description}' WHERE `aid` ={$id} LIMIT 1 ;");
			unset($description);
			$array[2][$i]=preg_replace(array("/<p.*?>/i"),array("<p>　　"),strip_tags($array[2][$i],"<p><br>"));
			mysql_query("INSERT INTO `content` ( `aid` , `content` ) VALUES ('{$id}', '{$array[2][$i]}');");

			unset($array[2][$i]);
			}
		}
		mysql_query("UPDATE `word` SET `updatetime` = NOW( ) WHERE `wid` ={$wid} LIMIT 1 ;");

	}
}
function fetweb_soso($word,$wid,$url,$pattern,$utf8)
{
	global $keynumlimit,$keynummost,$keynumrate,$contentnummost,$contentnumlimit;
	$array=caiji($url,$pattern,$utf8);
	
	//echo $url;print_r($array);
	if($array&&is_array($array))
	{
		$n=count($array[0]);$k=0;
		for ($i=0;$i<$n;$i++)
		{
			/*
			 $titlewordnum=substr_count($array[0][$i],$word);//内容关键词个数
			 if($titlewordnum!=1) continue;//如果标题中没有关键词不要
			 $wordnum=substr_count($array[2][$i],$word);//内容关键词个数
			 if($wordnum<$keynumlimit||$wordnum>$keynummost) continue;//如果内容不匹配关键词的情况
			 $contentnum=strlen($array[2][$i]);//内容长度
			 if($contentnum>$contentnummost||$contentnum<$contentnumlimit) continue;//如果内容过长或过少
			 if(($wordnum/$contentnum)>$keynumrate) continue;//如果关键词密度过大
			 */
			$array[0][$i]=strip_tags(html_entity_decode($array[0][$i]));
			//if(!preg_match("/[\w\n]+/",$array[0][$i])) continue;//如果标题含有非英文
			//if(!preg_match("/[\w\n]+/",$array[3][$i])) continue;//如果正文含有非英文

			//$array[0][$i]=str_replace(array("!",":","|","),array(" "),$array[0][$i]);标题
			$array[0][$i]=str_replace(array("\"","...","."),array("-",""),$array[0][$i]);
			$array[3][$i]=strip_tags(html_entity_decode($array[3][$i]));
			$pinyin=str_replace(array(" ","...",".",":","\""),"-",substr($array[0][$i],0,20));

			mysql_query("INSERT INTO `article` ( `aid` , `wid` , `title` , `author` , `description`,`src`,`pubtime`,`pinyin` ) VALUES (NULL , '{$wid}', '{$array[0][$i]}', '{$array[5][$i]}', '','{$array[1][$i]}',now(),'{$pinyin}');");
			$id=mysql_insert_id();
			unset($array[0][$i],$array[3][$i],$pinyin);
			if($id)
			{	$k++;
			$array[2][$i]=html_entity_decode($array[2][$i]);
			$description=substr(strip_tags($array[2][$i]),0,200);
			mysql_query("UPDATE `article` SET `description` = '{$description}' WHERE `aid` ={$id} LIMIT 1 ;");
			unset($description);
			$array[2][$i]=preg_replace(array("/<p.*?>/i"),array("<p>　　"),strip_tags($array[2][$i],"<p><br>"));
			mysql_query("INSERT INTO `content` ( `aid` , `content` ) VALUES ('{$id}', '{$array[2][$i]}');");

			unset($array[2][$i]);
			}
		}
		mysql_query("UPDATE `word` SET `updatetime` = NOW( ) WHERE `wid` ={$wid} LIMIT 1 ;");

	}
}
function cnsubstr($string,$sublen)
{
	if($sublen>=strlen($string))
	{
		return $string; } $s="";
		for($i=0;$i<$sublen;$i++)
		{
			if(ord($string{$i})>127)
			{
				$s.=$string{$i}.$string{++$i};
				continue;
			}else{
				$s.=$string{$i};
				continue;
			}
		}
		return $s;
}
function pagelistnum($sql,$pagenum,$pid,$purl,$nurl,$navnum=5)
{
	$rs=@mysql_fetch_row(mysql_query($sql));
	$titleall=$rs[0];
	$pageall=ceil($titleall/$pagenum);
	$c="Page {$pid} ";
	$begin=$pid-$navnum<1?1:$pid-$navnum;
	$end=$pid+$navnum>$pageall?$pageall:$pid+$navnum;
	$newbegin=$begin-($navnum-($end-$pid));
	$newend=$end+($navnum-($pid-$begin));
	$newbegin=$newbegin<1?1:$newbegin;
	$newend=$newend>$pageall?$pageall:$newend;
	for ($i=$newbegin;$i<=$newend;$i++)
	{
		$c.=$i==$pid?" <b>{$i}</b> ":" <a href=\"{$purl}{$i}{$nurl}\">{$i}</a> ";
	}
	$c.="{$titleall}/{$pageall}/{$pagenum} ";

	return $c;
}
?>